package com.smile.commons.gps;

import com.alibaba.fastjson.JSONObject;
import com.smile.commons.gps.domain.CoordinatePointReq;
import com.smile.commons.gps.domain.MappingTypeReq;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * WPMZ文件生成器测试类
 * 验证面状航线vs线状航线的修复效果
 */
public class WpmzGeneratorTest {
    
    public static void main(String[] args) {
        System.out.println("=== WPMZ文件生成器测试 ===");
        System.out.println("\n1. 检验MappingTypeReq属性设置");
        
        // 测试MappingTypeReq所有属性设置
        MappingTypeReq mappingTypeReq = new MappingTypeReq();
        
        // 设置基本属性
        mappingTypeReq.setCollectionMethod("ortho");
        mappingTypeReq.setLensType("camera");
        mappingTypeReq.setOverlapH(80);
        mappingTypeReq.setOverlapW(70);
        mappingTypeReq.setElevationOptimizeEnable(1);
        mappingTypeReq.setShootType("time");
        mappingTypeReq.setDirection("93");
        mappingTypeReq.setMargin("0");
        
        // 设置新增属性
        mappingTypeReq.setSmartObliqueEnable(0);
        mappingTypeReq.setQuickOrthoMappingEnable(0);
        mappingTypeReq.setFacadeWaylineEnable(0);
        mappingTypeReq.setIsLookAtSceneSet(0);
        mappingTypeReq.setEfficiencyFlightModeEnable(0);
        mappingTypeReq.setCaliFlightEnable(0);
        
        // 设置重叠率属性
        mappingTypeReq.setOrthoCameraOverlapH(80);
        mappingTypeReq.setOrthoCameraOverlapW(70);
        mappingTypeReq.setInclinedCameraOverlapH(80);
        mappingTypeReq.setInclinedCameraOverlapW(70);
        
        System.out.println("✓ 所有MappingTypeReq属性设置成功");
        
        try {
            testMappingAirlineGeneration();
        } catch (Exception e) {
            System.err.println("测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 测试面状航线生成
     */
    private static void testMappingAirlineGeneration() throws Exception {
        System.out.println("1. 测试面状航线WPMZ生成");
        
        // 1. 创建示例边界点（参考示例template.kml的坐标范围）
        List<CoordinatePointReq> boundaryPoints = createSampleBoundary();
        System.out.println("边界点数据: " + JSONObject.toJSONString(boundaryPoints));
        
        // 2. 生成面状航线
        PlanarAirlineGenerator.AirlineConfig airlineConfig = new PlanarAirlineGenerator.AirlineConfig();
        airlineConfig.setFlightHeight(new BigDecimal("134.262759171577")); // 使用示例的高度
        airlineConfig.setOverlapH(80);
        airlineConfig.setOverlapW(70);
        airlineConfig.setDirection(93); // 使用示例的方向
        airlineConfig.setMargin(0); // 使用示例的边距
        
        PlanarAirlineService.PlanarAirlineResult airlineResult = 
            PlanarAirlineService.generateCompletePlanarAirline(boundaryPoints, airlineConfig, 9.4);
        
        System.out.println("生成的航线摘要: " + PlanarAirlineService.generateAirlineSummary(airlineResult));
        System.out.println("航点数量: " + airlineResult.getFlightPath().size());
        
        // 3. 测试面状航线WPMZ生成（修复前的方法）
        System.out.println("\n2. 生成面状航线WPMZ文件（可能边框不正确）");
        
        WpmzFileGenerator.WpmzConfig config = new WpmzFileGenerator.WpmzConfig();
        config.setFileName("test_mapping2d_airline_old");
        config.setTemplateType("mapping2d"); // 确保是面状航线
        config.setFlightHeight(new BigDecimal("134.262759171577"));
        config.setFlightSpeed(9.4);
        config.setAuthor("15528435863"); // 使用示例的作者
        
        WpmzFileGenerator.WpmzGenerationResult result = 
            WpmzFileGenerator.generateWpmzFromFlightPath(airlineResult.getFlightPath(), config);
        
        System.out.println("旧方法WPMZ生成结果:");
        System.out.println("文件路径: " + result.getWpmzFilePath());
        System.out.println("摘要: " + result.getSummary());
        System.out.println("模板类型: " + result.getUsedConfig().getTemplateType());
        
        // 4. 测试使用原始边界数据的面状航线WPMZ生成（修复后的方法）
        System.out.println("\n3. 生成面状航线WPMZ文件（使用原始边界数据 - 推荐方法）");
        
        WpmzFileGenerator.WpmzConfig configWithBoundary = new WpmzFileGenerator.WpmzConfig();
        configWithBoundary.setFileName("test_mapping2d_airline_with_boundary");
        configWithBoundary.setTemplateType("mapping2d"); // 确保是面状航线
        configWithBoundary.setFlightHeight(new BigDecimal("134.262759171577"));
        configWithBoundary.setFlightSpeed(9.4);
        configWithBoundary.setAuthor("15528435863");
        // 注意：这里假设 boundaryPoints 使用的是高德地图坐标（GCJ02）
        configWithBoundary.setEnableCoordinateConversion(true);
        configWithBoundary.setSourceCoordinateSystem("GCJ02"); // 高德地图坐标
        configWithBoundary.setTargetCoordinateSystem("WGS84"); // 转换为 GPS 坐标
        
        WpmzFileGenerator.WpmzGenerationResult resultWithBoundary = 
            WpmzFileGenerator.generateWpmzFromAirlineResultWithBoundary(
                airlineResult, boundaryPoints, configWithBoundary);
        
        System.out.println("新方法WPMZ生成结果（含坐标转换）:");
        System.out.println("文件路径: " + resultWithBoundary.getWpmzFilePath());
        System.out.println("摘要: " + resultWithBoundary.getSummary());
        System.out.println("模板类型: " + resultWithBoundary.getUsedConfig().getTemplateType());
        System.out.println("坐标系转换: " + resultWithBoundary.getUsedConfig().getSourceCoordinateSystem() + " -> " + resultWithBoundary.getUsedConfig().getTargetCoordinateSystem());
        
        // 5. 测试不启用坐标转换的面状航线
        System.out.println("\n4. 生成面状航线WPMZ文件（不转换坐标系）");
        
        WpmzFileGenerator.WpmzConfig configNoConversion = new WpmzFileGenerator.WpmzConfig();
        configNoConversion.setFileName("test_mapping2d_airline_no_conversion");
        configNoConversion.setTemplateType("mapping2d");
        configNoConversion.setFlightHeight(new BigDecimal("134.262759171577"));
        configNoConversion.setFlightSpeed(9.4);
        configNoConversion.setAuthor("15528435863");
        configNoConversion.setEnableCoordinateConversion(false); // 不启用坐标转换
        
        WpmzFileGenerator.WpmzGenerationResult resultNoConversion = 
            WpmzFileGenerator.generateWpmzFromAirlineResultWithBoundary(
                airlineResult, boundaryPoints, configNoConversion);
        
        System.out.println("不转换坐标系WPMZ结果:");
        System.out.println("文件路径: " + resultNoConversion.getWpmzFilePath());
        System.out.println("摘要: " + resultNoConversion.getSummary());
        
        // 6. 对比测试：生成线状航线WPMZ
        System.out.println("\n5. 对比测试：生成线状航线WPMZ文件");
        
        WpmzFileGenerator.WpmzConfig waypointConfig = new WpmzFileGenerator.WpmzConfig();
        waypointConfig.setFileName("test_waypoint_airline");
        waypointConfig.setTemplateType("waypoint"); // 线状航线
        waypointConfig.setFlightHeight(new BigDecimal("134.262759171577"));
        waypointConfig.setFlightSpeed(9.4);
        // 线状航线也可以启用坐标转换
        waypointConfig.setEnableCoordinateConversion(true);
        waypointConfig.setSourceCoordinateSystem("GCJ02");
        waypointConfig.setTargetCoordinateSystem("WGS84");
        
        WpmzFileGenerator.WpmzGenerationResult waypointResult = 
            WpmzFileGenerator.generateWpmzFromFlightPath(airlineResult.getFlightPath(), waypointConfig);
        
        System.out.println("线状航线WPMZ生成结果（含坐标转换）:");
        System.out.println("文件路径: " + waypointResult.getWpmzFilePath());
        System.out.println("摘要: " + waypointResult.getSummary());
        System.out.println("模板类型: " + waypointResult.getUsedConfig().getTemplateType());
        System.out.println("坐标系转换: " + waypointResult.getUsedConfig().getSourceCoordinateSystem() + " -> " + waypointResult.getUsedConfig().getTargetCoordinateSystem());
        
        // 7. 输出总结
        System.out.println("\n=== 测试总结 ===");
        System.out.println("成功生成 3 个WPMZ文件：");
        System.out.println("1. 面状航线（旧方法，可能边框不正确）: " + result.getWaypointCount() + " 个航点");
        System.out.println("2. 面状航线（新方法，使用原始边界）: " + resultWithBoundary.getWaypointCount() + " 个航点");
        System.out.println("3. 线状航线: " + waypointResult.getWaypointCount() + " 个航点");
        System.out.println();
        System.out.println("✅ 关键修复：");
        System.out.println("   - 新增 generateWpmzFromAirlineResultWithBoundary 方法");
        System.out.println("   - 使用原始边界数据而不是从航点推导边界");
        System.out.println("   - 确保面状航线的边框正确显示测区范围");
        System.out.println();
        System.out.println("📝 使用建议：");
        System.out.println("   - 面状航线请使用 generateWpmzFromAirlineResultWithBoundary");
        System.out.println("   - 线状航线可以继续使用 generateWpmzFromFlightPath");
    }
    
    /**
     * 创建示例边界点（基于示例template.kml的坐标）
     */
    private static List<CoordinatePointReq> createSampleBoundary() {
        List<CoordinatePointReq> points = new ArrayList<>();
        
        // 使用示例template.kml中的边界坐标
        points.add(createPoint(39.6801091512935, 118.135943483423));
        points.add(createPoint(39.679790690293, 118.131196548465));
        points.add(createPoint(39.6815623289153, 118.130211181177));
        points.add(createPoint(39.6829881081781, 118.133507192001));
        points.add(createPoint(39.6827112812614, 118.13906045686));
        
        return points;
    }
    
    /**
     * 创建坐标点
     */
    private static CoordinatePointReq createPoint(double lat, double lon) {
        CoordinatePointReq point = new CoordinatePointReq();
        point.setLatitude(new BigDecimal(lat));
        point.setLongitude(new BigDecimal(lon));
        point.setHeight(new BigDecimal("134.262759171577"));
        return point;
    }
}